﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Stores.Interface;
using Microhard.Plat.CRM.Stores.Models;
using Microhard.Plat.CRM.Stores.DataAccess;
using Microhard.Plat.CRM.Stores.DTO;
using Microhard.Plat.CRM.Stores.Msg;

namespace Micrhard.FMCG.Server.Impl
{
    public class IspTemplateImpl : IIspTemplate
    {
        public bool IsNameDuplicate(IspTemplateModel model)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var cnt = db.CheckListTemplates.Count(c => c.CompanyID == model.CompanyID && c.TemplateName == model.TemplateName);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(IspTemplateModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var op = model.ToEntity();

            try
            {
                op.CreateTime = DateTime.Now;
                op.CompanyID = this.scope.EmpInfo.CompanyID;
                db.CheckListTemplates.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, IspTemplateMsg.AddFailed);
                Log.Error("IIspTemplate.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(IspTemplateModel model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplates.SingleOrDefault(l => l.CheckListTemplateID == model.CheckListTemplateID);
            if (entity != null)
            {
                entity.TemplateName = model.TemplateName;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, IspTemplateMsg.UpdateFailed);
                Log.Error("IIspTemplate.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var entity = db.CheckListTemplates.Where(l => ids.Contains(l.CheckListTemplateID));
            foreach (var e in entity)
            {
                //模板是否在使用
                if (e.CheckListTemplateCategories.Count == 0 && e.CheckListSheets.Count == 0)
                {
                    db.CheckListTemplates.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(IspTemplateMsg.TemplateIsInUsing, e.TemplateName));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, IspTemplateMsg.DeleteFailed);
                    Log.Error("ICheckListTemplate.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(IspTemplateModel model)
        {
            throw new NotImplementedException();
        }

        public List<IspTemplateModel> GetAllTemplate()
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            IEnumerable<CheckListTemplate> entities = from b in db.CheckListTemplates
                                                      select b;
            if (entities.Count() > 0)
            {
                return entities.Select(m => m.ToModel()).ToList();
            }

            return new List<IspTemplateModel>();
        }
        public IspTemplateModel GetModelByID(Guid id)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            var mcmp = db.CheckListTemplates.SingleOrDefault(c => c.CheckListTemplateID == id);
            if (mcmp != null)
            {
                return mcmp.ToModel();
            }
            return null;
        }

        public IspTemplateModel GetModelByModel(IspTemplateModel model)
        {
            throw new NotImplementedException();
        }

        public List<IspTemplateModel> Query(IspTemplateModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            Microhard_Plat_StoresEntities db = new Microhard_Plat_StoresEntities();
            IEnumerable<CheckListTemplate> entities = from b in db.CheckListTemplates
                                                      where b.CompanyID == this.scope.EmpInfo.CompanyID
                                                      select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
            {
                return pagedEntities.Select(m => m.ToModel()).ToList();
            }
            return new List<IspTemplateModel>();
        }

        public List<IspTemplateModel> Query(IspTemplateModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
